package fr.cie.blog.modeldao.hib;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import fr.cie.blog.beans.Avatar;
import fr.cie.blog.beans.Card;
import fr.cie.blog.domainservice.IServiceAvatar;

/**
 * @author AetM
 * 
 */
public class AvatarDaoImplHib extends HibernateDaoSupport implements
		IServiceAvatar {

	/**
	 * Log4J the logger.
	 */
	private static final Logger LOG = Logger
			.getLogger(AvatarDaoImplHib.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#save(java.lang.Object)
	 */
	@Override
	public final int save(final Avatar avatar) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.saveOrUpdate(avatar);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("update Avatar error", e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#update(java.lang.Object)
	 */
	@Override
	public final int update(final Avatar avatar) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.saveOrUpdate(avatar);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("update Avatar error", e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#delete(java.lang.Object)
	 */
	@Override
	public final int delete(final Avatar avatar) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.delete(avatar);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("delete Avatar error", e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#findAll()
	 */
	@Override
	public final List<Avatar> findAll() {
		@SuppressWarnings("unchecked")
		final List<Avatar> list = (List<Avatar>) getHibernateTemplate().find(
				"from Avatar");
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.cie.blog.domainservice.IServiceAvatar#findByCard(fr.cie.blog.beans
	 * .Card)
	 */
	@Override
	public final Avatar findByCard(final Card pCard) {
		return findById(pCard.getAvatar().getId());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#findById(int)
	 */
	@Override
	public final Avatar findById(final long pIdentifier) {
		return getHibernateTemplate().get(Avatar.class, pIdentifier);
	}

	@Override
	public final Avatar findByName(final String pAvatarName) {
		@SuppressWarnings("unchecked")
		final List<Avatar> avatarList = (List<Avatar>) getHibernateTemplate()
				.find("from Avatar a where a.name = ?", pAvatarName);
		Avatar avatar = null;
		if (!(avatarList == null || avatarList.isEmpty())) {
			avatar = avatarList.get(0);
		}
		return avatar;
	}

}